<template>
  <el-dialog :close-on-click-modal="false" append-to-body :title="title" :visible.sync="visible" width="800px">
    <div class="add-form">
      <el-form ref="form" :model="form" label-width="120px" :disabled="type === 'view'">
        <el-form-item label="服务类型">
          <CategorySelect :whileCodeList="['MAGIC', 'FLOWER', 'GIANT_PANDA', 'DELIVERY_MANAGER', 'MAKE']" size="small" v-model="form.type" placeholder="服务类型"></CategorySelect>
        </el-form-item>
        <el-form-item label="联系人" prop="linkName" :rules="[
          { required: true, message: '请选择联系人', trigger: 'blur' }
          ]">
          <el-button size="small" type="primary" plain @click="onSelectUser('link')">{{ form.linkName || '选择联系人' }}
          </el-button>
        </el-form-item>
        <el-form-item label="联系人启用项">
          <el-checkbox v-model="form.enableLinkCall" :true-label="1" :false-label="0">电话</el-checkbox>
          <el-checkbox v-model="form.enableLinkSend" :true-label="1" :false-label="0">短信</el-checkbox>
        </el-form-item>
        <el-form-item label="联系人电话" prop="linkMobile" :rules="[
          { required: true, message: '请输入联系人电话', trigger: 'blur' },
          { validator: FormValidateMap.validatePhone, trigger: 'blur' }
        ]">
          <el-input v-model="form.linkMobile" placeholder="请输入联系人电话"></el-input>
        </el-form-item>
        <el-form-item label="联系人邮箱" prop="linkEmail" :rules="[
          { validator: FormValidateMap.validateEmail, trigger: 'blur' }
        ]">
          <el-input v-model="form.linkEmail" placeholder="请输入联系人邮箱"></el-input>
        </el-form-item>
        <el-form-item label="分仓" prop="warehouseId" :rules="[
          { required: true, message: '请选择分仓', trigger: 'change' }
        ]">
          <xm-select :options="warehouseList" v-model="form.warehouseId" placeholder="请选择" filterable clearable>
          </xm-select>
        </el-form-item>
        <el-form-item label="仓库负责人" prop="manageName">
          <el-button size="small" type="primary" plain @click="onSelectUser('manage')">{{ form.manageName || '选择仓库负责人' }}
          </el-button>
          <el-button v-show="form.manageName" @click="handleRemoveManageUser" size="small" type="text" icon="el-icon-delete"></el-button>
        </el-form-item>
        <el-form-item label="负责人启用项">
          <el-checkbox v-model="form.enableManageCall" :true-label="1" :false-label="0">电话</el-checkbox>
          <el-checkbox v-model="form.enableManageSend" :true-label="1" :false-label="0">短信</el-checkbox>
        </el-form-item>
        <el-form-item label="负责人联系电话" prop="manageMobile" :rules="[
          { validator: FormValidateMap.validatePhone, trigger: 'blur' }
        ]">
          <el-input v-model="form.manageMobile" placeholder="请输入负责人联系电话"></el-input>
        </el-form-item>
        <el-form-item label="负责人邮箱" prop="manageEmail" :rules="[
          { validator: FormValidateMap.validateEmail, trigger: 'blur' }
        ]">
          <el-input v-model="form.manageEmail" placeholder="请输入负责人邮箱"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="submit" v-show="type !== 'view'" :loading="loading">{{ loading ? '提交中...' :
          '提交'
      }}</el-button>
    </span>

    <selectUserDialog ref="selectUserDialog" @select="handleSelectUser"></selectUserDialog>
  </el-dialog>
</template>

<script>
import { saveAddedServices, updateAddedServices } from '@/api/system/addedServices'
import { getWarehouseListByCityId } from "@/api/pending";
import { FormValidateMap } from '@/util/validate'
import selectUserDialog from '@/components/dialog/selectUserDialog'
import { mapGetters } from "vuex";
export default {
  components: {
    selectUserDialog
  },
  data() {
    return {
      FormValidateMap,
      visible: false,
      loading: false,
      type: 'add',
      form: {
        "linkName": "",
        "linkMobile": "",
        "linkEmail": "",
        enableLinkCall: 1,
        enableLinkSend: 1,
        "manageName": "",
        "manageMobile": "",
        "manageEmail": "",
        enableManageCall: 1,
        enableManageSend: 1,
        "type": 1,
        "warehouseId": ''
      },
      warehouseList: []
    }
  },
  computed: {
    ...mapGetters(['addedServicesTypeList']),
    title() {
      const textMap = {
        'add': '新 增',
        'edit': '编 辑',
        'view': '查 看',
      }
      return textMap[this.type]
    }
  },
  methods: {
    show({ type, data }) {
      this.type = type
      this.visible = true
      if (data) {
        this.form = {
          ...data,
        }
      } else {
        this.clearForm()
      }
    },
    clearForm() {
      this.form = {
        "linkName": "",
        "linkMobile": "",
        "linkEmail": "",
        enableLinkCall: 1,
        enableLinkSend: 1,
        "manageName": "",
        "manageMobile": "",
        "manageEmail": "",
        enableManageCall: 1,
        enableManageSend: 1,
        "type": 1,
        "warehouseId": ''
      }
      this.$refs.form && this.$refs.form.clearValidate()
    },
    submit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const request = this.type === 'add' ? saveAddedServices : updateAddedServices
          this.loading = true
          request(this.getParams()).then(res => {
            if (res.data.code === 200) {
              this.$message({
                type: 'success',
                message: `提交成功！`
              })
              this.visible = false
              this.$emit('submit')
            } else {
              this.$message({
                type: 'error',
                message: res.data.msg
              })
            }
          }).finally(() => {
            this.loading = false
          })
        }
      })
    },
    getParams() {
      const params = {
        ...this.form,
      }
      return params
    },
    getWarehouseListByCityId() {
      getWarehouseListByCityId({ cityId: 0 }).then(res => {
        this.warehouseList = (res.data.data || []).map(item => {
          return {
            ...item,
            value: item.id + '',
            label: `${item.name} - ${item.deptName}`  
          }
        })
      })
    },
    onSelectUser(type) {
      this.selectUserType = type
      this.$refs.selectUserDialog.show(type)
    },
    handleSelectUser(user) {
      if (this.selectUserType === 'link') {
        this.form.linkName = user.account
        this.form.linkMobile = user.phone
        this.form.linkEmail = user.email
      } else if (this.selectUserType === 'manage') {
        this.form.manageName = user.account
        this.form.manageMobile = user.phone
        this.form.manageEmail = user.email
      }
    },
    handleRemoveManageUser() {
      this.form.manageName = ''
    }
  },
  created() {
    this.getWarehouseListByCityId()
  }
}
</script>

<style lang="scss" scoped>
.add-form {}
</style>
